<script setup>
import {ref, reactive} from 'vue'
import { useUserStore } from '@/stores/userStore.js';
import RoleList from '@/components/RoleList/Index.vue'


const userStore = useUserStore();

const dialogVisible = ref(false)

const user = reactive({
  username: '',
  realName: '',
  mobile: '',
  email: '',
  userType: '',
  status: '',
  roleIds: [],
  userId: '',
  deptId: ''
});

const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
  userType: [{ required: true, message: '请选择用户类型', trigger: 'change' }],
  status: [{ required: true, message: '请选择用户状态', trigger: 'change' }]
}

const userForm = ref(null)


const submitForm = () => {
  userForm.value?.validate().then( () => {
    console.log("校验通过", user.value);
    userStore.updateUserInfo({
      ...user
    })
  }).catch( () => {
    console.log("校验失败");
  })
  close();

}

const show = (userInfo) => {
  Object.assign(user, userInfo);
  user.userId = userInfo.id;
  dialogVisible.value = true;
}

const close = () => {
  dialogVisible.value = false;
}

const save = () => {
  submitForm();

}

//对外暴露show
defineExpose({
  show
})
</script>

<template>
<el-dialog
    title="用户编辑"
    v-model="dialogVisible"
    width="50%"
>
  <el-form :model="user" :rules="rules" ref="userForm" label-width="100px">
  <el-form-item label="账号" prop="username">
    <el-input v-model="user.username" placeholder="请输入账号"></el-input>
  </el-form-item>
  <el-form-item label="姓名" prop="realName">
    <el-input v-model="user.realName" placeholder="请输入姓名"></el-input>
  </el-form-item>
  <el-form-item label="手机号" prop="mobile">
    <el-input v-model="user.mobile" placeholder="请输入手机号"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
  </el-form-item>
  <el-form-item label="用户类型" prop="userType">
    <el-select v-model="user.userType" placeholder="请选择用户类型">
      <el-option label="普通用户" value="USER"></el-option>
      <el-option label="管理员" value="ADMIN"></el-option>
    </el-select>
  </el-form-item>
    <!--角色  -->
    <el-form-item label="角色" prop="roleId">
      <RoleList v-model="user.roleIds" :roleIds="user.roleIds"></RoleList>
    </el-form-item>
  <el-form-item label="状态" prop="status">
    <el-select v-model="user.status" placeholder="请选择状态">
      <el-option label="正常" :value="1"></el-option>
      <el-option label="禁用" :value="2"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item >
    <el-button type="primary" @click="submitForm">确定</el-button>
    <el-button @click="close">取消</el-button>
  </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped>

</style>